package com.service.single.example.pattern.observer;

import java.util.ArrayList;
import java.util.List;


/**
 * 被观察者
 */
public class Subject {

    private final List<Observer> observers = new ArrayList<>();

    public void setState(int state) {
        this.update(state);
    }

    public void addObserver(Observer observer) {
        this.observers.add(observer);
    }

    private void update(int state) {
        for (Observer observer : observers) {
            observer.update(state);
        }
    }

    public static void main(String[] args) {
        Subject subject = new Subject();
        new OneObserver(subject);
        new TwoObserver(subject);
        new ThreeObserver(subject);
        subject.update(1);
    }
}
